home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8938 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  59 lines

  1. Newsgroups: comp.lang.c
  2. Path: peer-news.britain.eu.net!warwick!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: do || die;
  5. Message-ID: <DnwJKs.KyJ@uns.bris.ac.uk>
  6. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  7. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  8. Organization: Inmos
  9. X-Newsreader: TIN [version 1.2 PL2]
  10. References: <1996Mar7.052636.59812@ucl.ac.uk>
  11. Date: Thu, 7 Mar 1996 14:43:40 GMT
  12.  
  13. Timothy Slidel (slidel@bsm.bioc.ucl.ac.uk) wrote:
  14. : Whilst it seems to work ok - is it considered bad style to use
  15. : logical operator expressions as conditional statements on their own, a la
  16. : Perl?
  17.  
  18. : e.g. if I want to decrement i only when it is != 0:
  19.  
  20. : i && i--;
  21.  
  22. You should write to make your intent clear.
  23.  
  24. The expressions 'i && i--' looks like a simple boolean conditional.
  25. However, your intent is the sideffect of i--. As such
  26.     if(i)
  27.         i--;
  28. would be much clearer.
  29.  
  30. Suppose I have a processing function which, if it fails I want to set an
  31. error flag. What is the important thing here? setting the error flag
  32. or processing the data? probably processing the data. Which of the following
  33. two code fragments more clearly conveys what is happening?
  34.  
  35.     if(processing_function())
  36.         error = 1;
  37. or
  38.     error |= processing_function();
  39.  
  40. Another example. Suppose I want to set a variable to one of two expressions
  41. based on a flag? Is the thing to emphases the test or the assignment?
  42.  
  43.     if(flag)
  44.         var = expr1;
  45.     else
  46.         var = expr2;
  47. or
  48.     var = flag ? expr1 : expr2
  49.  
  50. It depends on circumstances (and therein lies the art).
  51.  
  52. nathan
  53.  
  54. --
  55. Nathan Sidwell                         Holder of the Xmris home page
  56. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  57. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  58. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  59.